﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Rendering;
using NetCoreCMS.Common;
using NetCoreCMS.IServices;
using NetCoreCMS.Models;
namespace NetCoreCMS.Web.Areas.Admin.Controllers
{
    public class Recharge_EditController : ManageController
    {
        private const string WEB_VIEW = "~/Areas/Admin/Views/Users/Recharge_Edit.cshtml";
        private string username = string.Empty;
        public IPaymentService _payemnt { get; set; }
        public IUser_rechargeService _user_recharge { get; set; }
        public IUsersService _users { get; set; }
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            if (filterContext.Result != null)
                return;
            ChkAdminLevel("user_recharge_log", CMSEnums.ActionEnum.View.ToString()); //检查权限
            this.username = Request.Query["username"]; //获得用户名
            ViewBag.UserName = this.username;
            string rechargeNo = Utils.GetOrderNumber();//随机生成订单号
            ViewBag.RechargeNo = rechargeNo;
        }
        //
        // GET: /admin/Recharge_Edit/

        public ActionResult Index()
        {
            TreeBind("where type=1 and is_lock=0"); //绑定支付方式
            return View(WEB_VIEW);
        }

        [HttpPost]
        public ActionResult SubmitSave()
        {
            ChkAdminLevel("user_recharge_log", CMSEnums.ActionEnum.Add.ToString()); //检查权限
            if (!DoAdd())
            {
                ViewBag.ClientScript = JscriptMsg("保存过程中发生错误！", "");
            }
            else
            {
                ViewBag.ClientScript = JscriptMsg("会员充值成功！", "../recharge_list/index");
            }
            return View(EDIT_RESULT_VIEW);
        }

        #region 绑定支付方式=============================
        private void TreeBind(string strWhere)
        {
            var dt = _payemnt.GetList(strWhere+" order by sort_id asc,id asc");
            List<SelectListItem> list = new List<SelectListItem>();
            list.Add(new SelectListItem() { Value = "", Text = "请选择支付方式" });
            foreach (Payment dr in dt)
            {
                list.Add(new SelectListItem() { Value = dr.id.ToString(), Text = dr.title });
            }
            ViewData["list"] = list;
        }
        #endregion

        #region 增加操作=================================
        private bool DoAdd()
        {
            var userModel = _users.GetModel(Request.Form["txtUserName"].FirstOrDefault().Trim());
            if (userModel == null)
            {
                return false;
            }
            bool result = false;
            User_recharge model = new User_recharge();
            model.user_id = userModel.id;
            model.user_name = userModel.user_name;
            model.recharge_no = "R" + Request.Form["txtRechargeNo"].FirstOrDefault().Trim(); //订单号R开头为充值订单
            model.payment_id = Utils.StrToInt(Request.Form["ddlPaymentId"], 0);
            model.amount = Utils.StrToDecimal(Request.Form["txtAmount"].FirstOrDefault().Trim(), 0);
            model.status = 1;
            model.complete_time = DateTime.Now;
            if (_user_recharge.Recharge(model))
            {
                AddAdminLog(CMSEnums.ActionEnum.Add.ToString(), "给会员：" + model.user_name + "，充值:" + model.amount + "元"); //记录日志
                result = true;
            }
            return result;
        }
        #endregion
    }
}
